package org.testis.gui.mvc.controller.testdesign;

import org.testis.gui.mvc.controller.*;
import org.testis.gui.mvc.model.testdesign.ProfileSettingsSelectionModel;
import org.testis.gui.mvc.utilities.AtofTableModel;
import org.testis.gui.mvc.view.testdesign.ProfileSettingsSelectionView;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.testis.orm.classes.T_test_profiles;
import org.testis.utils.middleware.GlobalDataHandler;

/**
 *
 * @author nurhat
 */
public class ProfileSettingsSelectionController extends AbstractController {

    public static final String SHOW_VIEW = "ViewVisibility";
    public static final String PARAMETER_MODEL_CHANGED = "Model";
    public static final String SHOW_ERROR = "LastError";
    private String m_key;
    private String m_value;
    private boolean m_isRemove;

    public ProfileSettingsSelectionController() {
        super();
        TestDesignControllerList.newInstance().setProfileSettingsSelectionController(this);
    }

    public boolean operationApplyToProfile(int profileId) {
        GlobalDataHandler handler = new GlobalDataHandler();
        if (!m_isRemove) {
            return handler.saveProfileDetail(profileId, m_key, m_value);
        } else {
            return handler.removeProfileDetail(profileId, m_key);
        }
    }

    public boolean operationApplyToProfile(int[] profileIdList) {
        GlobalDataHandler handler = new GlobalDataHandler();
        if (!m_isRemove) {
            return handler.saveProfileDetail(profileIdList, m_key, m_value);
        } else {
            return handler.removeProfileDetail(profileIdList, m_key);
        }
    }

    public void operationCreateCopy(int profileId) {
        GlobalDataHandler handler = new GlobalDataHandler();
        T_test_profiles profile = handler.getProfileById(profileId);
        HashMap hmap = handler.getProfileDetailsAsMap(profileId);
        String newName = "Copy of " + profile.getName();
        int newProfileId = handler.saveProfile(newName);
        if (newProfileId > 0) {
            Iterator iter = hmap.entrySet().iterator();
            while (iter.hasNext()) {
                Map.Entry entry = (Map.Entry) iter.next();
                handler.saveProfileDetail(newProfileId,
                        entry.getKey().toString(),
                        entry.getValue().toString());
            }
            operationLoadProfiles();
        } else {
            setModelProperty(SHOW_ERROR,
                    "Unable to copy record...");
        }
    }

    public void startLoader(String key, String value, boolean remove) {
        m_key = key;
        m_value = value;
        m_isRemove = remove;
        initialize();
        setModelProperty(SHOW_VIEW, true);
    }

    public void operationCloseWindow() {
        this.cleanAll();
    }

    public void initialize() {
        AtofTableModel temp = new AtofTableModel();
        temp.addColumn("Id");
        temp.addColumn("Name");
        ProfileSettingsSelectionModel model = new ProfileSettingsSelectionModel(temp);
        ProfileSettingsSelectionView view = new ProfileSettingsSelectionView(TestDesignControllerList.newInstance().
                getParentFrame(),
                true,
                temp);
        super.addModel(model);
        super.addView(view);
        view.pack();
    }

    public HashMap operationGetProfiles() {
        GlobalDataHandler handler = new GlobalDataHandler();
        HashMap hmap = handler.getProfilesAsMap();
        return hmap;
    }

    public void operationLoadProfiles() {
        GlobalDataHandler handler = new GlobalDataHandler();
        HashMap hmap = handler.getProfilesAsMap();
        AtofTableModel temp = new AtofTableModel();
        temp.addColumn("Id");
        temp.addColumn("Name");
        Iterator iter = hmap.entrySet().iterator();
        while (iter.hasNext()) {
            Map.Entry entry = (Map.Entry) iter.next();
            String[] keyVal = new String[2];
            keyVal[0] = entry.getKey().toString();
            keyVal[1] = entry.getValue().toString();
            temp.addRow(keyVal);

        }
        setModelProperty(PARAMETER_MODEL_CHANGED,
                temp);
    }

    public void operationAddProfile(String name) {
        GlobalDataHandler handler = new GlobalDataHandler();
        boolean result = handler.saveProfile(0,
                name);
        if (result) {
            operationLoadProfiles();
        } else {
            setModelProperty(SHOW_ERROR,
                    "Unable to add record...");
        }
    }

    public void operationEditProfile(int id,
            String name) {
        GlobalDataHandler handler = new GlobalDataHandler();
        boolean result = handler.saveProfile(id,
                name);
        if (result) {
            operationLoadProfiles();
        } else {
            setModelProperty(SHOW_ERROR,
                    "Unable to edit record...");
        }
    }

    public void operationRemoveProfile(int id) {
        GlobalDataHandler handler = new GlobalDataHandler();
        boolean result = handler.removeProfile(id);
        if (result) {
            operationLoadProfiles();
        } else {
            setModelProperty(SHOW_ERROR, "Unable to remove record...");
        }
    }
}
